package com.apes.fn.server.productReturn;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import com.apes.scm.masterdata.stock.model.Location;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.*;

/***
 * 商品归还单
 * 1. 领用单手动归还
 * 2. 销售自动归还
 */


@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ProductReturn.CONST_TABLE_NAME)
public class ProductReturn extends Model {

    public static final String CONST_TABLE_NAME = "stock_product_return";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "归还单号", prefix = "PG%(company)%(yy)%(month)", padding = 5, useDateRange=true)
    @Comment("归还单号")
    private String id; //收入单号

    @Column(name = "source", length = 30)
    @Comment("来源单号")
    private String source;

    @Column(name = "source_business_type", length = 32)
    @Comment("来源单据类型")
    private String sourceBusinessType;

    @Column(name = "source_business_type_name", length = 64)
    @Comment("来源单据类型名称")
    private String sourceBusinessTypeName;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "location_id", referencedColumnName = "id")
    @Comment("库存位置")
    private Location location;

//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "customer_id", referencedColumnName = "id")
//    private PartyRole customer; //客户编码（包含客户组 客户类别）


//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "receive_id", referencedColumnName = "id")
//    @Comment("领用人员")
//    private PartyRole receiver;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id", nullable = false)
    @Comment("公司")
    private PartyRole company;

    @Column(name = "remark", length = 200)
    @Comment("备注")
    private String remark;

    @States(name = "状态", values = {
            @State(id = "create", name = "创建"),
            @State(id = "approve", name = "已审核"),
            @State(id = "delete", name = "删除"),
    })
    @Column(name = "state")
    @Comment("状态(create:创建,approve:审核,delete:删除)")
    private String state;

    @Column(name = "scan_sign", nullable = false, columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("扫码标记")
    private boolean scanSign = false;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_id", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole approveId; //审核

    @Comment("审批日期")
    @Column(name = "approve_date")
    private Date approveDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "delete_id", referencedColumnName = "id")
    @Comment("删除人")
    private PartyRole deleteId; //删除

    @Column(name = "delete_date")
    @Comment("删除日期")
    private Date deleteDate; //删除日期

    @OneToMany(mappedBy = "productReturn", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<ProductReturnItem> productReturnItems = new ArrayList();

    public List<ProductReturnItem> getProductReturnItems() {
        for (ProductReturnItem item : productReturnItems) {
            // 设置主表以及主表公司
            item.setProductReturn(this);
            item.setCompany(this.company);
        }
        return productReturnItems;
    }


    @Transient
    private PartyRole _creator;

    public PartyRole get_creator() {
        if(this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }

}
